﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq; 
using NTTDATA.BEENChina.ResultCertification.Config;
using NTTDATA.BEENChina.ResultCertification.PropertyGridUtil;

namespace NTTDATA.BEENChina.ResultCertification.Checker.Numeric
{
   
    [Serializable]
    [VerfityAttribute(Id = "VerfityD0102", Name = "N组日期合并到一起是递增的一组数时抽出检证", CategoryType = EnumCategoryType.数值, Model = true)]
    public class VerfityD0102 : VerfityOneResult
    {       
        int _joinColumns = 0;

        [Category("设定合并检查的列"), Description("设定合并检查的列")]
        [EditorAttribute(typeof(PropertyGridRichText), typeof(System.Drawing.Design.UITypeEditor))]
        public int JoinColumns
        {
            get { return _joinColumns; }
            set { _joinColumns = value; }
        }

        [Category("升序检查标记"), Description("true:升序检查 false:非升序检查标记")]
        public bool Asc { get; set; }

        //private List<int> GetJoinColumns()
        //{
        //    var rows = _joinColumns.Split(new char[] { ',' });

        //        List<int> emptyRows = new List<int>();
        //        foreach (string row in rows)
        //        {
        //            int r;

        //            if (!int.TryParse(row, out r))
        //            {
        //                r = -1;
        //            }

        //            emptyRows.Add(r);
        //        }

        //        return emptyRows;
            
        //}

        private string GetSpeicficResult(string colResult)
        {
            var splitResult = new Split.SplitEntryResult(ChildImageObject.CategorySetting.DataFormat, colResult);
            return splitResult.GetMd();
        }

        public override bool CheckResult(List<List<string>> result, string entryCommon)
        {
            bool bresult = false;
            if (result == null)
            {
                return false;
            }
            
            for (int i = 0; i < result.Count; i++)
            {
                List<string> checkResult = null;
                for (int j = 0; j < JoinColumns; j++)
                {
                    if (i + j < result.Count)
                    {
                        foreach (string colResult in result[i + j])
                        {
                            string md = GetSpeicficResult(colResult);

                            if (!string.IsNullOrEmpty(md))
                            {
                                //if (!string.IsNullOrEmpty(checkResult))
                                //    checkResult += ".";

                                //checkResult += md; 
                                if (checkResult == null)
                                    checkResult = new List<string>();

                                checkResult.Add(md);
                            }
                        }                    
                    }
                }

                if (checkResult != null && checkResult.Count.Equals(JoinColumns))
                {
                    bresult = CheckSort( string.Join(".", checkResult));
                }

                if (bresult)
                {
                    break;
                }
            }
           

            if (bresult)
            {
                System.Console.WriteLine("VerfityD0102");
            }

            return bresult;
        }

        private bool CheckSort( string result)
        {
            bool bresult = false;

            //var joinRows = GetJoinColumns();

            //string joinMdResult = string.Empty;

            ////找到最大行数
            //int maxRow  = joinRows.Max();

            ////需要合并的最大行小于总行数
            //if (result.Count < maxRow)
            //{
            //    return false;
            //}
      
            //foreach (int row in joinRows)
            //{
            //    var splitResult = new Split.SplitEntryResult(ChildImageObject.CategorySetting.DataFormat, result[row]);
            //    joinMdResult += splitResult.GetMd();

            //    //bresult =  bresult && result[emptyRow].Any(colStr => string.IsNullOrEmpty(colStr.Replace("/", string.Empty)));
            //}

            if (!String.IsNullOrEmpty(result))
            {
                var joinMds = result.Split(new char[] { '.' }).Where(o => !o.Contains("?")).ToList();

                if (joinMds.Count() <= 1)
                {
                    return false;
                }
                var sortResult = joinMds.OrderBy(t => t, new NumericComparer()).ToList();

                bool bsortResult = joinMds.SequenceEqual(sortResult);
                //
                if (Asc)
                {
                    if (bsortResult)
                        bresult = true;
                }
                else
                {
                    if (!bsortResult)
                        bresult = true;
                }
            }
            return bresult;
        }


    }
}
